package org.example.controller;

import org.example.feign.BrandFeignClient;
import org.example.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/brand")
public class AdminBrandController {

    @Autowired
    private BrandFeignClient brandFeignClient;

    /**
     * 品牌列表页面
     */
    @GetMapping("/list")
    public String brandList(Model model) {
        try {
            ResponseEntity<List<Brand>> response = brandFeignClient.queryAllBrand();
            if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
                model.addAttribute("brands", response.getBody());
            } else {
                model.addAttribute("brands", new ArrayList<>());
                model.addAttribute("error", "获取品牌列表失败");
            }
        } catch (Exception e) {
            model.addAttribute("brands", new ArrayList<>());
            model.addAttribute("error", "服务调用异常: " + e.getMessage());
        }
        return "brand/list";
    }

    /**
     * 添加品牌页面
     */
    @GetMapping("/add")
    public String addBrandPage(Model model) {
        model.addAttribute("brand", new Brand());
        return "brand/add";
    }

    /**
     * 处理添加品牌代码
     */
    @PostMapping("/add")
    public String addBrand(@ModelAttribute Brand brand, RedirectAttributes redirectAttributes) {
        try {
            ResponseEntity<String> response = brandFeignClient.addBrand(brand);
            if (response.getStatusCode().is2xxSuccessful()) {
                redirectAttributes.addFlashAttribute("success", "品牌添加成功");
            } else {
                redirectAttributes.addFlashAttribute("error", "品牌添加失败");
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "服务调用异常: " + e.getMessage());
        }
        return "redirect:/admin/brand/list";
    }

    /**
     * 编辑品牌页面
     */
    @GetMapping("/edit/{id}")
    public String editBrandPage(@PathVariable Integer id, Model model) {
        try {
            ResponseEntity<Brand> response = brandFeignClient.queryBrandById(id);
            if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
                model.addAttribute("brand", response.getBody());
            } else {
                model.addAttribute("error", "品牌不存在");
                return "redirect:/admin/brand/list";
            }
        } catch (Exception e) {
            model.addAttribute("error", "服务调用异常: " + e.getMessage());
            return "redirect:/admin/brand/list";
        }
        return "brand/edit";
    }

    /**
     * 处理编辑品牌
     */
    @PostMapping("/edit/{id}")
    public String editBrand(@PathVariable Integer id, @ModelAttribute Brand brand, RedirectAttributes redirectAttributes) {
        try {
            ResponseEntity<String> response = brandFeignClient.updateBrand(id, brand);
            if (response.getStatusCode().is2xxSuccessful()) {
                redirectAttributes.addFlashAttribute("success", "品牌更新成功");
            } else {
                redirectAttributes.addFlashAttribute("error", "品牌更新失败");
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "服务调用异常: " + e.getMessage());
        }
        return "redirect:/admin/brand/list";
    }

    /**
     * 删除品牌
     */
    @PostMapping("/delete/{id}")
    public String deleteBrand(@PathVariable Integer id, RedirectAttributes redirectAttributes) {
        try {
            ResponseEntity<String> response = brandFeignClient.deleteBrand(id);
            if (response.getStatusCode().is2xxSuccessful()) {
                redirectAttributes.addFlashAttribute("success", "品牌删除成功");
            } else {
                redirectAttributes.addFlashAttribute("error", "品牌删除失败");
            }
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "服务调用异常: " + e.getMessage());
        }
        return "redirect:/admin/brand/list";
    }

    /**
     * 首页重定向到品牌列表
     */
    @GetMapping("/")
    public String index() {
        return "redirect:/admin/brand/list";
    }
}